/*
 * cTxtParser.java
 *
 * Created on 21. juin 2007, 10:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pacman;

import java.io.*;
import java.util.ArrayList;

/**
 * A map parser. Parses text maps as provided by default with game.
 * @author julien
 */
public class cTxtParser implements iParser {
    
    private ArrayList<iFood> FoodList;
    private ArrayList<iGhost> GhostList;
    private ArrayList<cPacman> PacmanList;
    private iCase[][] plateau;
    private cCore parent;
    
    /**
     * Creates a new instance of cTxtParser
     * @param parent The core where the parser is used.
     */
    public cTxtParser(cCore parent) {
        this.parent=parent;
    }

    /**
     * Returns the supported extensions for this parser.
     * @return A string containing the extension supported by this parser.
     */
    public String getSupportedExt() {
        return "tmm";
    }

    /**
     * Loads the map contained in specified file in maps folder.
     * @param FileName The name of the file containing the map to be loaded.
     */
    public void LoadMap(String FileName) {
        FoodList=new ArrayList<iFood>();
        GhostList=new ArrayList<iGhost>();
        PacmanList=new ArrayList<cPacman>();
               
        
        LoadDimentions(FileName);
        
        File file=new File(FileName);
        StringBuffer contents = new StringBuffer();
        BufferedReader input = null;
        String line = null;
        int nb=0;
        
        try {
            input = new BufferedReader( new FileReader(FileName) );
            try {
                
                while (( line = input.readLine()) != null){
                    parseLine(line,nb);
                    nb++;
                }
                
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }

    }
    
    private void parseLine(String line, int nb){
        for(int i=0;i<plateau.length-2;i++){
            switch (line.charAt(i)){
                case '#':
                    plateau[i+1][nb+1]=new cWall();
                    break;
                case 't':
                    plateau[i+1][nb+1]=new cTeleporter(parent);
                    break;
                case 'g':
                    plateau[i+1][nb+1]=new cCaseGPL();
                    break;
                case 'o':
                    plateau[i+1][nb+1]=new cWay();
                    FoodList.add(new cFish(new Position(i+1,nb+1),parent));
                    break;
                case 'p':
                    plateau[i+1][nb+1]=new cWay();
                    PacmanList.add(new cPacman(parent,new Position(i+1,nb+1)));
                    break;
                case 'b':
                    plateau[i+1][nb+1]=new cWay();
                    GhostList.add(new cBlueGhost(parent,new Position(i+1,nb+1)));
                    break;
                case 'r':
                    plateau[i+1][nb+1]=new cWay();
                    GhostList.add(new cRedGhost(parent,new Position(i+1,nb+1)));
                    break;
                case 'y':
                    plateau[i+1][nb+1]=new cWay();
                    GhostList.add(new cYellowGhost(parent,new Position(i+1,nb+1)));
                    break;
                default:
                    plateau[i+1][nb+1]=new cWay();
                    break;
            }
        }
    }
    
    private void LoadDimentions(String FileName){
        int DimX=0;
        
        File file=new File(FileName);
        StringBuffer contents = new StringBuffer();
        BufferedReader input = null;
        String line = null;
        int nb=0;
        
        try {
            input = new BufferedReader( new FileReader(FileName) );
            try {
                
                while (( line = input.readLine()) != null){
                    if(nb==0){
                        DimX=line.length();
                    }else{
                        if(DimX>line.length()){
                            DimX=line.length();
                        }
                    }
                    
                    nb++;
                }
                
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        
        plateau=new iCase[DimX+2][nb+2];
        
        for(int i=0;i<plateau.length;i++){
            for(int j=0;j<plateau[i].length;j++){
                if(i==0 || i==plateau.length-1 || j==0 || j==plateau[i].length-1){
                    plateau[i][j]=new cWall();
                }
            }
        }
    }
    
    /**
     * Returns a list of iFood found on the loaded map.
     * @return An ArrayList of iFood found on the map.
     */
    public ArrayList<iFood> getFoodList() {
        return FoodList;
    }

    /**
     * Returns a list of ghosts found on the map.
     * @return An ArrayList of iGhost found on the map.
     */
    public ArrayList<iGhost> getGhostList() {
        return GhostList;
    }
    
    /**
     * Returns a list of pacmen found on the map.
     * @return An ArrayList of pacmen found on the map.
     */
    public ArrayList<cPacman> getPacmanList(){
        return PacmanList;
    }

    /**
     * Returns the map found in the file parsed.
     * @return A array containing the map found in the file parsed.
     */
    public iCase[][] getMap() {
        return plateau;
    }

  
    
}
